package cn.edu.scnu.thewonderofink

import android.annotation.SuppressLint
import android.content.pm.ActivityInfo
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import cn.edu.scnu.thewonderofink.databinding.ActivityMainBinding
import cn.edu.scnu.thewonderofink.logic.network.RecognitionNetwork
import cn.edu.scnu.thewonderofink.ui.home.HomeFragment
import cn.edu.scnu.thewonderofink.ui.viewpage2.ViewPager2Adapter

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    @SuppressLint("SourceLockedOrientationActivity")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)

        setContentView(binding.root)
        // 去除自带的选中颜色,去除后文字和图片选择效果就是跟我们自定义的效果一样
        binding.navView.itemIconTintList = null

        // 切换竖屏（针对模拟器）
        if (requestedOrientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }


        //将所有的Fragment添加到ViewPager2中
        val fragmentList: MutableList<Fragment> = ArrayList()
        fragmentList.add(HomeFragment())
        // fragmentList.add(DictionaryFragment())
        // fragmentList.add(MeFragment())
        binding.navViewpage2.adapter = ViewPager2Adapter(this, fragmentList)

        // 当viewpage2页面切换时nav导航图标也跟着切换
        binding.navViewpage2.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.navView.menu.getItem(position).isChecked = true
            }
        })

        // 当nav导航点击切换时，ViewPager2也跟着切换页面
        binding.navView.setOnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.navigation_home -> {
                    binding.navViewpage2.currentItem = 0
                    return@setOnItemSelectedListener true
                }

//                R.id.navigation_dictionary -> {
//                    binding.navViewpage2.currentItem = 1
//                    return@setOnItemSelectedListener true
//                }

            }
            false
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        RecognitionNetwork.close()
    }

}